$_SERVER['HTTP_REFERER'] not working with Header

Posted by EmmyS on Stack Overflow See other posts from Stack Overflow or by EmmyS
Published on 2010-05-14T23:36:38Z Indexed on 2010/05/14 23:44 UTC
Read the original article Hit count: 198

Filed under:

I have a site that allows public access to some pages, but requires a login for others. I have a link to the login from all pages, and what I'd like to do after a successful login is send the user back to the page they were on when they clicked the login link. I know the HTTP_REFERER can be spoofed, and sometimes stripped out by certain hosts and proxies, but since it's strictly within my own site, and only a convenience for users, I'm not too worried about it.

I am curious about why it isn't working in conjunction with a redirect, though. I've set a visible field to contain the value of the http referer, and it displays correctly. So the page is getting the value of the referrer variable. But when I try this:

$home_url =  $_SERVER['HTTP_REFERER'];
header('Location: ' . $home_url);

it doesn't work. This, on the other hand, does:

$home_url = 'http://' . $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/discussions.php';
header('Location: ' . $home_url);

So I know the header location part works. Any idea why it doesn't want to work in conjunction with the http_referer variable?

(Also, does it drive anyone else nuts that referer is spelled incorrectly? I keep mistyping it using the OED spelling, silly me...)

© Stack Overflow or respective owner

Related posts about php